package com.centaline.androidsalesblog.activities.news;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.mine.LoginActivity;
import com.centaline.androidsalesblog.adapter.TabFragmentAdapter;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.SprfConstant;
import com.centaline.androidsalesblog.events.JPushMessage;
import com.centaline.androidsalesblog.fragments.list.EsfNewsFragment;
import com.centaline.androidsalesblog.fragments.list.PostNewsFragment;
import com.centanet.framework.interfaces.ISnack;
import com.centanet.framework.utils.SprfUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 动态
 * Created by guilin on 16/3/30.
 */
public class EstateNewsActivity extends BaseActivity {

    private ViewPager viewPager;
    private TabLayout tabLayout;

    private ISnack iSnack = new ISnack() {
        @Override
        public void showSnack(CharSequence text) {
            snack(text);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.activity_estate_news;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar();
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    @Override
    protected void initViews() {
        SprfUtil.setBoolean(this, String.format(Locale.CHINA, "%s_%s", AppConstant.CityCode, SprfConstant.JPUSH_NEW_MESSAGE),false);
        EventBus.getDefault().post(new JPushMessage(""));
        checkLogin();
    }

    private void checkLogin() {
        if (AppConstant.IsLogin) {
            NotificationManagerCompat.from(this).cancelAll();
            ArrayList<String> titleList = new ArrayList<>();
            ArrayList<Fragment> fragments = new ArrayList<>();
            titleList.add("房源动态");
            titleList.add("小区动态");
            PostNewsFragment postNewsFragment = new PostNewsFragment();
            postNewsFragment.setISnack(iSnack);
            fragments.add(postNewsFragment);
            EsfNewsFragment esfNewsFragment = new EsfNewsFragment();
            esfNewsFragment.setISnack(iSnack);
            fragments.add(esfNewsFragment);
            viewPager.setAdapter(new TabFragmentAdapter(getSupportFragmentManager(), fragments, titleList));
            tabLayout.setupWithViewPager(viewPager);
        } else {
            startActivityForResult(new Intent(this, LoginActivity.class), CommonConstant.REQ_LOGIN);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            switch (requestCode) {
                case CommonConstant.REQ_LOGIN:
                    checkLogin();
                    break;
                default:
                    break;
            }
        } else {
            finish();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
